﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;

namespace FIleForensics
{
    public partial class addUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            int isAdmin = 0;
            int isLocked = 0;

            string password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text, "sha1");
            string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Course Work 2\\FIleForensics\\App_Data\\FileForensic.mdf;Integrated Security=True;User Instance=True";

            if (chkAdmin.Checked)
            {
                isAdmin = 1;
            }

            if (chkLocked.Checked)
            {
                isLocked = 1;
            }

            SqlConnection conn = new SqlConnection(connString);

            SqlCommand addUser = new SqlCommand("INSERT INTO LogIn VALUES (@Username, @Password, @LastLogIn, @Admin, @Locked)", conn);
            addUser.Parameters.Add("@Username", SqlDbType.NVarChar).Value = this.txtUsername.Text;
            addUser.Parameters.Add("@Password", SqlDbType.NVarChar).Value = password;
            addUser.Parameters.AddWithValue ("@LastLogIn", DBNull.Value);
            addUser.Parameters.Add("@Admin", SqlDbType.Int).Value = isAdmin;
            addUser.Parameters.Add("@Locked", SqlDbType.Int).Value = isLocked;

            conn.Open();

            addUser.ExecuteNonQuery();

            conn.Close();
        }
    }
}